package com.laterem.rtc.exception;

import com.laterem.rtc.constant.HttpSatusCode;
import com.laterem.rtc.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.nio.file.AccessDeniedException;
import java.nio.file.attribute.UserPrincipalNotFoundException;

/**
 * @creator: alan wu
 * @dateTime: 2023/5/20 00:53
 * @desc:
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler({AccessDeniedException.class})
    public Result<Object> handAccessDeniedException(Exception e){
        return Result.fail(HttpSatusCode.NOT_AUTH, e.getMessage());
    }

    @ExceptionHandler({UserPrincipalNotFoundException.class})
    public Result<Object> handUserPrincipalNotFoundException(Exception e){
        return Result.fail(HttpSatusCode.ERROR, e.getMessage());
    }

    @ExceptionHandler({RuntimeException.class})
    public Result<Object> handRuntimeException(Exception e){
        log.debug("报错信息：{}", e.getMessage());
        return Result.fail(HttpSatusCode.ERROR, e.getMessage());
    }
}
